//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// SA_XEnhancer X effect.txt file 
// COPYRIGHT - ZIMMER // BORIS VORONTSOV // ZEROKING // ICELAGLACE
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
//--------------------------//
//   Internal parameters    // 
//     Can be modified	    //
//--------------------------// 

//////////////////
//    DEFINES   
//////////////////				4

#include "xenhancer_config.txt"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		
//--------------------------//
//   External parameters	// 
//     Do not modify		//
//--------------------------//

////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Keyboard controlled temporary variables. 
// Press and hold key 1,2,3...8 together with PageUp or PageDown to modify. 
// By default all set to 1.0
float4	tempF1; 			//0,1,2,3
float4	tempF2; 			//5,6,7,8
float4	tempF3; 			//9,0
float4	Timer;				//x=generic timer in range 0..1, period of 16777216 ms (4.6 hours), w=frame time elapsed (in seconds)
float4	ScreenSize;			//x=Width, y=1/Width, z=ScreenScaleY, w=1/ScreenScaleY
float	ENightDayFactor;	//changes in range 0..1, 0 means that night time, 1 - day time
float	EInteriorFactor;	//changes 0 or 1. 0 means that exterior, 1 - interior
////////////////////////////////////////////////////////////////////////////////////////////////////////////////

//////////////////
//	TEXTURES	//
//////////////////
texture2D texColor;
texture2D texNoise;

///////////////////
//	 SAMPLERS	 //
///////////////////
sampler2D SamplerColor = sampler_state
{
	Texture   = <texColor>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = NONE;
	AddressU  = Clamp;
	AddressV  = Clamp;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

sampler2D SamplerNoise = sampler_state
{
	Texture   = <texNoise>;
	MinFilter = LINEAR;
	MagFilter = LINEAR;
	MipFilter = NONE;
	AddressU  = Wrap;
	AddressV  = Wrap;
	SRGBTexture=FALSE;
	MaxMipLevel=0;
	MipMapLodBias=0;
};

//////////////////
//	 STRUCTS	//
//////////////////
struct VS_OUTPUT_POST
{
	float4 vpos  : POSITION;
	float2 txcoord : TEXCOORD0;
};

struct VS_INPUT_POST
{
	float3 pos  : POSITION;
	float2 txcoord : TEXCOORD0;
};

VS_OUTPUT_POST VS_PostProcess(VS_INPUT_POST IN)
{
	VS_OUTPUT_POST OUT;

	float4 pos=float4(IN.pos.x,IN.pos.y,IN.pos.z,1.0);

	OUT.vpos=pos;
	OUT.txcoord.xy=IN.txcoord.xy;

	return OUT;
}

//////////////////
//	 SHADERS	//
//////////////////

float4 PS_ProcessNoise(VS_OUTPUT_POST IN, float2 vPos : VPOS) : COLOR
{
float NoiseAmount = NoiseAmountExt;
float NoiseFineness = NoiseFinenessExt;
if (EInteriorFactor) 
{
NoiseAmount = NoiseAmountInt;
NoiseFineness = NoiseFinenessInt;
};
float4 coord=0.0;
coord.xy=IN.txcoord.xy;
coord.w=0.0; 
#define noise_strength_luma 				(CoefLuma * NoiseAmount)  
#define CoefLuma 							float4(0.2126, 0.7152, 0.0722, 0)
float4 tcol=tex2Dlod(SamplerColor, coord); 
float origgray=max(tcol.x, tcol.y);
origgray=max(origgray, tcol.z);
coord.xy=IN.txcoord.xy*NoiseFineness + frac(Timer.x * 100000.0 + Timer.x);
float4 cnoi=tex2Dlod(SamplerNoise, coord);
#if NoiseMethod == 1
tcol=lerp(tcol, (cnoi.x+0.5)*tcol, noise_strength_luma*saturate(1.0-origgray*0.5));
#endif
#if NoiseMethod == 0
tcol=lerp(tcol, (cnoi.x+0.5)*tcol, NoiseAmount*saturate(1.0-origgray*0.5));
#endif
tcol.w=1.0;
return tcol;
}

float4 LensDistortion(VS_OUTPUT_POST t,float2 i:VPOS):COLOR{float d;float2 f=t.txcoord.xy;
#if (LENSDISTORTION==1)
float2 S=float2(.5,.5),r=f-S;float z=sqrt(dot(r,r)),a=-tempF2.z*3.14159/(2.*sqrt(dot(S,S)))*(1./ScreenSize.x-.5);if(a>0.)d=sqrt(dot(S,S));else{if(a<1.)d=S.x;else d=S.y;}float2 y;if(a>0.)y=S+normalize(r)*tan(z*a)*d/tan(d*a);else if(a<0.)y=S+normalize(r)*atan(z*-a*10.)*d/atan(-a*d*10.);else y=f;float4 o=tex2D(SamplerColor,y);
#else
float4 o=tex2D(SamplerColor,f);
#endif
return o;
}

float4 Final(VS_OUTPUT_POST f,float2 z:VPOS):COLOR{float2 t=f.txcoord.xy;float3 r=tex2D(SamplerColor,t).xyz;
#if (CINEMA==1)
r.xyz=t.y>.1&&t.y<.9?r.xyz:0.;
#endif
return float4(r,1);
}

///////////////////////
//	  TECHNIQUES	 //
///////////////////////
technique PostProcess
{
    pass P0
    {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 PS_ProcessNoise();

DitherEnable=FALSE;
ZEnable=FALSE;
CullMode=NONE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
StencilEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
}

technique PostProcess2
{
   pass P0
   {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 LensDistortion();

FogEnable=FALSE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
}

technique PostProcess3
{
   pass P0
   {
VertexShader = compile vs_3_0 VS_PostProcess();
PixelShader  = compile ps_3_0 Final();

FogEnable=FALSE;
ALPHATESTENABLE=FALSE;
SEPARATEALPHABLENDENABLE=FALSE;
AlphaBlendEnable=FALSE;
FogEnable=FALSE;
SRGBWRITEENABLE=FALSE;
}
}